package com.ns.school.controller.edu;

import com.github.pagehelper.PageHelper;
import com.ns.school.common.utils.CommonUtil;
import com.ns.school.common.utils.JsonUtil;
import com.ns.school.po.edu.BookStockDB;
import com.ns.school.po.edu.LayuiResult;
import com.ns.school.service.edu.BookStockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 图书库存管理
 */
@Controller
@Slf4j
public class BookStockController {
    @Autowired
    private BookStockService bookStockService;

    @RequestMapping("/bookStock/list")
    public String toList(Model model) {
        return "view/bookStock/list";
    }

    @RequestMapping("/bookStock/add")
    public String toAdd(Model model) {
        return "view/bookStock/add";
    }

    @RequestMapping("/bookStock/edit")
    public String toEdit(Model model, Integer id, ModelMap modelMap) {
        BookStockDB bookStockDB =bookStockService.selectBookStock(id);// 此处需要通过参数id获取到具体的图书对象
        log.info(JsonUtil.toStr(bookStockDB));
        modelMap.put("bookStock",bookStockDB);
        return "view/bookStock/edit";
    }

    //入库操作
    @RequestMapping("addBookStock")
    @ResponseBody
    public LayuiResult<BookStockDB> addBookStock(@RequestBody BookStockDB bookStockDB) {
        LayuiResult<BookStockDB> result = new LayuiResult<>();
        bookStockService.addBookStockMapper(bookStockDB,result);
        return result;

    }

    //库存查询
    //获取库存列表
    @RequestMapping("findBookStock")
    @ResponseBody
    public Object findBookStock(BookStockDB bookStockDB, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<BookStockDB> listAll = bookStockService.findBookStock(bookStockDB);
        return CommonUtil.getStringObjectMap(listAll);
    }



    //图书的审核
    @RequestMapping("auditBook")
    @ResponseBody
    public LayuiResult<BookStockDB> auditBooks(BookStockDB params) {
        LayuiResult result = new LayuiResult();
        if(params.getStockId()==null){
            result.setCode(500);
            result.setMsg("stockId不能为空");
            return result;
        }
        bookStockService.setBookStockMapper(result,params);
        return result;
    }

   //审核拒绝
    @RequestMapping("auditBookFail")
    @ResponseBody
    public LayuiResult<BookStockDB> auditFailBooks(BookStockDB bookStockDB) {
        LayuiResult result = new LayuiResult();
        bookStockService.auditFailMapper(result,bookStockDB);
        return result;
    }

    //图书入库编辑
    @RequestMapping("/updateBookStock")
    @ResponseBody
    public LayuiResult<BookStockDB> updateBookStock(@RequestBody BookStockDB bookStockDB) {
        LayuiResult result = new LayuiResult();
        log.info(JsonUtil.toStr(bookStockDB));
        bookStockService.updateBookStockMapper(result,bookStockDB);
        return result;
    }

}



